package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IMenu;
import org.agilelang.object.Controller;
import org.agilelang.object.Menu;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.MenuItem;


public class GMenu extends Generator implements IMenu {

    //String path = "";
    String appName = "";
    String header = "";
    String innav = "";
    String struts = "";
    //List<Controller> listController;
    //List<Menu> menus;
    String tiles = "";

    GMenu(String path_app, Project project) {
        this.path = path_app;
        this.menus = project.menus;
        this.controllers = project.controllers;
        this.template2="javastruts2";
    }

    public void process() {
        struts_controller();
        tiles_controller();
        header_controller();
        innav_controller();

        String file = readStreamTpl("me_tiles.xml.tpl");
        file = file.replace("//definition//", tiles);
        createfile("web/WEB-INF/tiles.xml", path, file);

        file = readStreamTpl("me_struts.xml.tpl");
        file = file.replace("//pakages//", struts);
        createfile("src/java/struts.xml", path, file);

        file = readStreamTpl("me_header.jsp.tpl");
        file = file.replace("//header//", header);
        createfile("/web/view/layout/header.jsp", path, file);

        file = readStreamTpl("me_innav.jsp.tpl");
        file = file.replace("//innav//", innav);
        createfile("/web/view/layout/innav.jsp", path, file);
    }

    public void tiles_controller() {
        int t = this.menus.size();
        for (int i = 0; i < t; i++) {
            Menu menu = this.menus.get(i);
            int tt = menu.menuitems.size();
            for (int j = 0; j < tt; j++) {
                MenuItem menuitem = menu.menuitems.get(j);
                if (menu.menuitems.get(j).action.toLowerCase().equals("crud")) {
                    tiles = tiles
                            + "\t<definition name=\"" + menu.menuitems.get(j).controller.toLowerCase() + ".index\" extends=\"baseLayout\">\r\n"
                            + "\t\t<put-attribute name=\"title\"  value=\"" + menu.menuitems.get(j).controller.toLowerCase() + " Index\"/>\r\n"
                            + "\t\t<put-attribute name=\"content\"   value=\"/view/" + menu.menuitems.get(j).controller.toLowerCase() + "/index.jsp\"/>\r\n"
                            + "\t</definition>\r\n";
                    tiles = tiles
                            + "\t<definition name=\"" + menu.menuitems.get(j).controller.toLowerCase() + ".read\" extends=\"baseLayout\">\r\n"
                            + "\t\t<put-attribute name=\"title\"  value=\"" + menu.menuitems.get(j).controller.toLowerCase() + " Read\"/>\r\n"
                            + "\t\t<put-attribute name=\"content\"   value=\"/view/" + menu.menuitems.get(j).controller.toLowerCase() + "/read.jsp\"/>\r\n"
                            + "\t</definition>\r\n";
                    tiles = tiles
                            + "\t<definition name=\"" + menu.menuitems.get(j).controller.toLowerCase() + ".add\" extends=\"baseLayout\">\r\n"
                            + "\t\t<put-attribute name=\"title\"  value=\"" + menu.menuitems.get(j).controller.toLowerCase() + " Add\"/>\r\n"
                            + "\t\t<put-attribute name=\"content\"   value=\"/view/" + menu.menuitems.get(j).controller.toLowerCase() + "/create.jsp\"/>\r\n"
                            + "\t</definition>\r\n";
                    tiles = tiles
                            + "\t<definition name=\"" + menu.menuitems.get(j).controller.toLowerCase() + ".edit\" extends=\"baseLayout\">\r\n"
                            + "\t\t<put-attribute name=\"title\"  value=\"" + menu.menuitems.get(j).controller.toLowerCase() + " Edit\"/>\r\n"
                            + "\t\t<put-attribute name=\"content\"   value=\"/view/" + menu.menuitems.get(j).controller.toLowerCase() + "/update.jsp\"/>\r\n"
                            + "\t</definition>\r\n";

                } else {
                    tiles = tiles
                            + "\t<definition name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "." + menu.menuitems.get(j).action.toLowerCase() + "\" extends=\"baseLayout\">\r\n"
                            + "\t\t<put-attribute name=\"title\"  value=\"" + menu.menuitems.get(j).controller.toLowerCase() + " " + menu.menuitems.get(j).action.toLowerCase() + "\"/>\r\n"
                            + "\t\t<put-attribute name=\"content\"   value=\"/view/" + menu.menuitems.get(j).controller.toLowerCase() + "/" + menu.menuitems.get(j).action.toLowerCase() + ".jsp\"/>\r\n"
                            + "\t</definition>\r\n";
                }
            }
        }
    }

    public void header_controller() {
        int t = this.menus.size();
        for (int i = 0; i < t; i++) {
            Menu menu = this.menus.get(i);
            Controller co = this.getControllerByName(menu.menuitems.get(0).controller);
            if (co.type.equals("scaffold") || co.type.equals("transaction")) {
                header = header
                        + "\t\t\t<li>\n"
                        + "\t\t\t\t<a href=\"<s:url value=\"/" + menu.name.toLowerCase() + "/" + menu.menuitems.get(0).controller.toLowerCase() + "index\"></s:url>\" <s:if test=\"#currenturl.contains('" + menu.name.toLowerCase() + "')\">class=\"selected\"</s:if>>" + menu.caption + "</a>\n"
                        + "\t\t\t</li>\n";
            } else {
                header = header
                        + "\t\t\t<li>\n"
                        + "\t\t\t\t<a href=\"<s:url value=\"/" + menu.name.toLowerCase() + "/" + menu.menuitems.get(0).action.toLowerCase() + "\"></s:url>\" <s:if test=\"#currenturl.contains('" + menu.name.toLowerCase() + "')\">class=\"selected\"</s:if>>" + menu.caption + "</a>\n"
                        + "\t\t\t</li>\n";
            }

        }
    }

    public void innav_controller() {
        String submenu = "";

        int t = this.menus.size();
        for (int i = 0; i < t; i++) {
            Menu menu = this.menus.get(i);
            int tt = menu.menuitems.size();
            submenu = "";
            for (int j = 0; j < tt; j++) {
                if (menu.menuitems.get(j).action.toLowerCase().equals("crud")) {
                    submenu = submenu + "\t\t\t\t<li><a id=\"tab-members\" href=\"<s:url value=\"/" + menu.name.toLowerCase() + "/" + menu.menuitems.get(j).controller.toLowerCase() + "index\"></s:url>\" <s:if test=\"#currenturl.endsWith('" + menu.menuitems.get(j).controller.toLowerCase() + "index') || #currenturl.endsWith('" + menu.menuitems.get(j).controller.toLowerCase() + "read') || #currenturl.endsWith('" + menu.menuitems.get(j).controller.toLowerCase() + "add') || #currenturl.endsWith('" + menu.menuitems.get(j).controller.toLowerCase() + "edit')\">class=\"selected\"</s:if><s:else>class=\"\"</s:else>>" + menu.menuitems.get(j).caption + "</a></li>\n";
                } else {
                    submenu = submenu + "\t\t\t\t<li><a id=\"tab-members\" href=\"<s:url value=\"/" + menu.name.toLowerCase() + "/" + menu.menuitems.get(j).action.toLowerCase() + "\"></s:url>\" <s:if test=\"#currenturl.endsWith('" + menu.menuitems.get(j).action.toLowerCase() + "')\">class=\"selected\"</s:if><s:else>class=\"\"</s:else>>" + menu.menuitems.get(j).caption + "</a></li>\n";
                }

            }
            innav = innav
                    + "\t<s:if test=\"#currenturl.contains('" + menu.name.toLowerCase() + "')\">\n"
                    + "\t\t<div class=\"tabs\">\n"
                    + "\t\t\t<ul>\n"
                    + submenu
                    + "\t\t\t</ul>\n"
                    + "\t\t</div>\n"
                    + "\t</s:if>\n";
        }
    }

    public void struts_controller() {
        String submenu = "";
        String submenu_ajax = "";
        int t = this.menus.size();

        for (int i = 0; i < t; i++) {
            Menu menu = this.menus.get(i);
            int tt = menu.menuitems.size();
            submenu = "";
            for (int j = 0; j < tt; j++) {
                if (menu.menuitems.get(j).action.toLowerCase().equals("crud")) {
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "index\" method=\"index\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"index\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".index</result>\n"
                            + "\t\t</action>\n";
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "read\" method=\"read\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"read\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".read</result>\n"
                            + "\t\t</action>\n";
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "add\" method=\"add\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"add\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".add</result>\n"
                            + "\t\t</action>\n";
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "edit\" method=\"edit\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"edit\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".edit</result>\n"
                            + "\t\t</action>\n";
                    Controller co = this.getControllerByName(menu.menuitems.get(j).controller);
                    if (co.type.equals("transaction")) {
                        submenu +=
                                "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "create\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\" method=\"create\">\n"
                                + "\t\t\t<result type=\"stream\">\n"
                                + "\t\t\t\t<param name=\"contentType\">text/html</param>\n"
                                + "\t\t\t\t<param name=\"inputName\">inputStream</param>\n"
                                + "\t\t\t</result>\n"
                                + "\t\t</action> \n";
                    } else {
                        submenu = submenu
                                + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "create\" method=\"create\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                                + "\t\t\t<result name=\"create\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".create</result>\n"
                                + "\t\t\t<result name=\"add\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".add</result>\n"
                                + "\t\t\t<result name=\"index\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".index</result>\n"
                                + "\t\t</action>\n";
                    }
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "update\" method=\"update\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"update\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".update</result>\n"
                            + "\t\t\t<result name=\"edit\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".edit</result>\n"
                            + "\t\t\t<result name=\"index\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".index</result>\n"
                            + "\t\t</action>\n";
                    submenu = submenu
                            + "\t\t<action name=\"" + menu.menuitems.get(j).controller.toLowerCase() + "delete\" method=\"delete\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                            + "\t\t\t<result name=\"delete\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".delete</result>\n"
                            + "\t\t\t<result name=\"index\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + ".index</result>\n"
                            + "\t\t</action>\n";
                } else {
                    String type = getTyeView(menu.menuitems.get(j).controller, menu.menuitems.get(j).action);
                    if (type.equals("ViewReport")) {
                        submenu = submenu
                                + "\t\t<action name=\"" + menu.menuitems.get(j).action.toLowerCase() + "PDF\" method=\"" + menu.menuitems.get(j).action.toLowerCase() + "PDF\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                                + "\t\t\t<result name=\"success\" type=\"stream\">\n"
                                + "\t\t\t\t<param name=\"contentType\">application/pdf</param>\n"
                                + "\t\t\t\t<param name=\"inputName\">inputStream</param>\n"
                                + "\t\t\t\t<param name=\"contentDisposition\">filename=\"report.pdf\"</param>\n"
                                + "\t\t\t\t<param name=\"bufferSize\">1024</param>\n"
                                + "\t\t\t</result>\n"
                                + "\t\t</action>\n";
                        
                        submenu = submenu
                                + "\t\t<action name=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" method=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                                + "\t\t\t<result name=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + "." + menu.menuitems.get(j).action.toLowerCase() + "</result>\n"
                                + "\t\t</action>\n";
                    } else {
                        submenu = submenu
                                + "\t\t<action name=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" method=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" class=\"controller." + menu.menuitems.get(j).controller + "Controller\">\n"
                                + "\t\t\t<result name=\"" + menu.menuitems.get(j).action.toLowerCase() + "\" type=\"tiles\">" + menu.menuitems.get(j).controller.toLowerCase() + "." + menu.menuitems.get(j).action.toLowerCase() + "</result>\n"
                                + "\t\t</action>\n";
                    }


                }


            }

            struts +=
                    "\t<package name=\"" + menu.name.toLowerCase() + "\" namespace=\"/" + menu.name.toLowerCase() + "\" extends=\"struts-default\">\n"
                    + "\t\t<result-types>\n"
                    + "\t\t\t<result-type name=\"tiles\" class=\"org.apache.struts2.views.tiles.TilesResult\" />\n"
                    + "\t\t</result-types>\n"
                    + submenu
                    + "\t</package>\n";
        }

        /*submenu_ajax =
         "\t<package name=\"struts2\" extends=\"struts-default\" namespace=\"/\"> \n"
         + submenu_ajax
         + "\t</package>\n";
        
         struts += submenu_ajax;*/
    }
}
